#include<stdio.h>
#include<assert.h>

typedef int ITEM;

typedef struct
{
    ITEM data;
    struct Node *next;
}Node;

typedef struct
{
    Node *ptop;
    int size;
    int Maxsize;
}LinkStack;

bool isEmpty(LinkStack *linkstack)
{
    assert(linkstack != NULL);
    return linkstack->size == 0;
}

bool isFull(LinkStack *linkstack)
{
    assert(linkstack != NULL);
    return linkstack->size == linkstack->Maxsize;
}

int getSize(LinkStack *linkstack)
{
    assert(linkstack != NULL);
    return linkstack->size;
}

ITEM *top(LinkStack *linkstack)
{
    assert(linkstack != NULL);
    return linkstack->ptop->data;
}

void initStack(LinkStack *linkstack, int maxsize)
{
    assert(linkstack != NULL);
    linkstack->size = 0;
    linkstack->Maxsize = maxsize;
    linkstack->ptop = NULL;
}

ITEM pop(LinkStack *linkstack)
{
    assert(linkstack != NULL);
    if (isEmpty(linkstack))
        return -1;
    Node *tmp = linkstack->ptop;
    ITEM data = tmp->data;
    linkstack->ptop = linkstack->ptop->next;
    linkstack->size--;
    free(tmp);
    return data;
}

bool push(LinkStack *linkstack, ITEM data)
{
    assert(linkstack != NULL);
    if (isFull(linkstack))
        return false;
    
    Node *tmp = (Node*)malloc(sizeof(Node));
    tmp->data = data;
    tmp->next = NULL;
    
    if(linkstack->ptop == NULL)
    {
        linkstack->ptop = tmp;
    }
    else
    {
        tmp->next = linkstack->ptop;
        linkstack->ptop = tmp;
    }
    linkstack->size++;
    return true;
}
int main(void) {
    LinkStack *linkstack = (LinkStack*)malloc(sizeof(LinkStack));
    initStack(linkstack, 10);
    for(int i=0; i<10; i++)
    {
        push(linkstack, i);
    }
    printf("linkstack size=%d\n", linkstack->size);
    while(linkstack->ptop != NULL)
    {
        printf("item = %d\n", pop(linkstack));
    }
    return 0;
}